﻿using System.Linq;
using www.Models;

namespace Data.DAO
{
    public class CategoryValueDAO//:BaseDAO<RELAXEntities,RelaxCategory>
   {

       #region value
       private readonly RelaxDataContext _context = new RelaxDataContext();
        readonly CategoryRefDAO _categoryRefDao=new CategoryRefDAO();
       #endregion

       #region Hàm lấy dữ liệu

       
       #endregion
       #region Add
       public void Add(CATEGORY_VALUE category)
       {
           _context.CATEGORY_VALUEs.InsertOnSubmit(category);
           _context.SubmitChanges();

       }
       #endregion
       #region Update

       public bool Update(CATEGORY_VALUE category)
       {
           var first = _context.CATEGORY_VALUEs.FirstOrDefault(c => c.categoryid == category.categoryid && c.languagecode==category.languagecode);
           if (first != null)
           {
               first.name = category.name;
               first.tag = category.tag;
               first.keyword = category.keyword;
               first.decription = category.decription;
               _context.SubmitChanges();
               return true;
           }
           return false;
       }
       #endregion

       #region Delete

       public void Delete(long iId,long iLanguageCode)
       {
           var obj = _context.CATEGORY_VALUEs.Where(c => c.categoryid == iId);
           var deleteRef = obj.Count() == 1 || iLanguageCode == 0;
           if (iLanguageCode != 0)
               obj = obj.Where(p => p.languagecode == iLanguageCode);
           foreach (var item in obj)
           {
               _context.CATEGORY_VALUEs.DeleteOnSubmit(item);
           }
           _context.SubmitChanges();
           if (deleteRef)
                _categoryRefDao.Delete(iId);
       }
       #endregion
   }
}
